//
//  MRNestContentViewController.m
//  MRDemo
//
//  Created by dingping on 2020/8/18.
//  Copyright © 2020 dingping. All rights reserved.
//

#import "MRNestContentViewController.h"
#import "MRNestCollectionViewCell.h"

static NSString *cellId = @"MRNestCollectionViewCell";
@interface MRNestContentViewController ()
<UICollectionViewDataSource,
UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UICollectionView *contentCollectionView;

@end


@implementation MRNestContentViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:self.contentCollectionView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(subScrollViewDidScroll:)]) {
        [self.delegate subScrollViewDidScroll:scrollView];
    }
}


#pragma mark - dataSource & delegate -
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 16;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MRNestCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{}


#pragma mark - init -
- (UICollectionView *)contentCollectionView {
    if (!_contentCollectionView) {
        _contentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-MRNavBarHeight) collectionViewLayout:self.layout];
        _contentCollectionView.backgroundColor = Main_BG_color;
        _contentCollectionView.delegate = self;
        _contentCollectionView.dataSource = self;
        _contentCollectionView.showsVerticalScrollIndicator = NO;
        _contentCollectionView.alwaysBounceVertical = YES;
        [_contentCollectionView registerClass:[MRNestCollectionViewCell class] forCellWithReuseIdentifier:cellId];
        _contentCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
    }
    return _contentCollectionView;
}

- (UICollectionViewFlowLayout *)layout {
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _layout.minimumLineSpacing = 15;
        _layout.minimumInteritemSpacing = 0;
        _layout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
        
        CGFloat itemWidth = ([UIScreen mainScreen].bounds.size.width - (2 + 1) *15 - 1) / 2.0;
        _layout.itemSize = CGSizeMake(itemWidth, itemWidth+97);
    }
    return _layout;
}

@end
